package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.OutStockBillMapper;
import com.canaan.business.domain.inOutStock.OutStockBill;
import com.canaan.business.service.inOutStock.IOutStockBillService;
import com.canaan.common.annotation.TableName;
/**
 * 出库单Service业务层处理
 *
 * @author canaan
 * @date 2025-03-29
 */
@Service
public class OutStockBillServiceImpl implements IOutStockBillService {
    @Resource
    private OutStockBillMapper outStockBillMapper;

    /**
     * 查询出库单
     *
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public OutStockBill selectOutStockBillById(Long id) {
        return outStockBillMapper.selectOutStockBillById(id);
    }

    /**
     * 查询出库单列表
     *
     * @param outStockBill 出库单
     * @return 出库单
     */
    @Override
    public List<OutStockBill> selectOutStockBillList(OutStockBill outStockBill) {
        return outStockBillMapper.selectOutStockBillList(outStockBill);
    }

    /**
     * 新增出库单
     *
     * @param outStockBill 出库单
     * @return 结果
     */
    @Override
    public int insertOutStockBill(OutStockBill outStockBill) {
        if(StringUtils.isBlank(outStockBill.getNo())){
            outStockBill.setNo("OUT"+TableNoUtil.getNo(OutStockBill.class));
        }
        outStockBill.setCreateBy(SecurityUtils.getUsername());
        outStockBill.setCreateTime(DateUtils.getNowDate());
        return outStockBillMapper.insertOutStockBill(outStockBill);
    }

    /**
     * 批量新增出库单
     *
     * @param outStockBillList 出库单列表
     * @return 结果
     */
    @Override
    public int batchInsertOutStockBill(List<OutStockBill> outStockBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (OutStockBill outStockBill : outStockBillList){
            if(StringUtils.isBlank(outStockBill.getNo())){
                outStockBill.setNo("OUT"+TableNoUtil.getNo(OutStockBill.class));
            }
            outStockBill.setCreateBy(username);
            outStockBill.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<outStockBillList.size();) {
            int endIndex = i+500;
            if (endIndex > outStockBillList.size()) {
                endIndex = outStockBillList.size();
            }
            insertNum = insertNum + outStockBillMapper.batchInsertOutStockBill(outStockBillList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改出库单
     *
     * @param outStockBill 出库单
     * @return 结果
     */
    @Override
    public int updateOutStockBill(OutStockBill outStockBill) {
        if(StringUtils.isEmpty(outStockBill.getUpdateBy())) {
            outStockBill.setUpdateBy(SecurityUtils.getUsername());
        }
        outStockBill.setUpdateTime(DateUtils.getNowDate());
        return outStockBillMapper.updateOutStockBill(outStockBill);
    }

    /**
     * 批量修改出库单
     *
     * @param outStockBillList 出库单列表
     * @return 结果
     */
    @Override
    public int batchUpdateOutStockBill(List<OutStockBill> outStockBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (OutStockBill outStockBill : outStockBillList){
            outStockBill.setUpdateBy(username);
            outStockBill.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<outStockBillList.size();) {
            int endIndex = i+500;
            if (endIndex > outStockBillList.size()) {
                endIndex = outStockBillList.size();
            }
            updateNum = updateNum + outStockBillMapper.batchUpdateOutStockBill(outStockBillList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除出库单
     *
     * @param ids 需要删除的出库单主键集合
     * @return 结果
     */
    @Override
    public int deleteOutStockBillByIds(Long[] ids) {
        return outStockBillMapper.deleteOutStockBillByIds(ids);
    }

    /**
     * 删除出库单信息
     *
     * @param id 出库单主键
     * @return 结果
     */
    @Override
    public int deleteOutStockBillById(Long id) {
        return outStockBillMapper.deleteOutStockBillById(id);
    }

    /**
     * 查询出库单
     *
     * @param no 编号
     * @return 出库单
     */
    @Override
    public OutStockBill selectOutStockBillByNo(String no) {
        return outStockBillMapper.selectOutStockBillByNo(no);
    }

    /**
     * 批量删除出库单
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteOutStockBillByNos(String[] nos) {
        return outStockBillMapper.deleteOutStockBillByNos(nos);
    }

    /**
     * 删除出库单信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteOutStockBillByNo(String no) {
        return outStockBillMapper.deleteOutStockBillByNo(no);
    }

    @Override
    public List<OutStockBill> selectOutStockBillByTrayNo(String no) {
        return outStockBillMapper.selectOutStockBillByTrayNo(no);
    }
    @Override
    public List<OutStockBill> selectEmptyDestOutStockBill(){
        return outStockBillMapper.selectEmptyDestOutStockBill();
    }
}
